﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Shapes;
using Silvermoon.OpenGL;
using System.Drawing;

namespace Silvermoon.Controls
{
    public class ImageTextItem : TextItem
    {
        private ImageShape imageShape;
        private static readonly Margin imagePadding = new Margin(8);

        public ImageTextItem()
            : base()
        {
            imageShape = new ImageShape();
            Shapes.AddFirst(imageShape);
        }

        public Texture Image
        {
            get { return imageShape.Image; }
            set
            {
                if (imageShape.Image != value)
                {
                    imageShape.Image = value;
                    OnImageChanged();
                }
            }
        }

        protected virtual void OnImageChanged()
        {
            Invalidate(InvalidationFlag.Layout);
        }

        protected override void OnLayout()
        {
            base.OnLayout();
            if (CheckFlag(InvalidationFlag.Size | InvalidationFlag.Layout | InvalidationFlag.Text))
            {
                if (Image != null)
                {
                    int left = Image.Width + imagePadding.Width;
                    label.Bounds = new Rectangle(label.X + left, label.Y, Math.Max(0, label.Width - left), label.Height);
                    int y = (Height - Image.Height) / 2;
                    imageShape.Bounds = new Rectangle(Padding.LeftZero, y, Image.Width, Image.Height);
                }
            }
        }
    }
}
